package com.qianduo.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.qianduo.dao.IQianduoOrderDao;
import com.qianduo.exception.DaoException;
import com.qianduo.po.QianduoOrder;
import com.qianduo.po.QianduoOrderView;
import com.qianduo.util.HibernateUtil;
import common.page.PageList;
import common.page.PaginationQuery;
import common.util.StringUtil;

@SuppressWarnings({ "finally", "unchecked" })
public class QianduoOrderDaoImpl extends HibernateUtil implements IQianduoOrderDao{
	public void saveOrUpdateOrder(QianduoOrder po) throws DaoException{
		try{
			super.saveOrUpdate(po);
		}catch(Exception e){
			throw new DaoException(className+" saveOrUpdateOrder",e);
		}
	}	
	
	public List<QianduoOrderView> listOrder(Integer id,Integer agencyID,String name,BigDecimal minAmount,BigDecimal maxAmount,Integer cityID,String mobile,Integer isMortgage,String mortgageType,Integer minAge,Integer maxAge,Integer wage,String createDateStart,String createDateEnd,PageList pageList) throws DaoException{
		List<QianduoOrderView> retList	= null;
		try{
			Criteria ca	= getSessionFactory().getCurrentSession().createCriteria(QianduoOrderView.class);
			if(null != id){
				ca.add(Restrictions.eq("id", id));
			}
			if(null != agencyID){
				ca.add(Restrictions.eq("agencyID", agencyID));
			}
			if(!StringUtil.isEmpty(name)){
				ca.add(Restrictions.like("name", name+"%"));
			}
			if(null != minAmount){
				ca.add(Restrictions.ge("amount", minAmount));
			}
			if(null != maxAmount){
				ca.add(Restrictions.le("amount", maxAmount));
			}
			if(null != cityID){
				ca.add(Restrictions.eq("cityID", cityID));
			}
			if(!StringUtil.isEmpty(mobile)){
				ca.add(Restrictions.like("mobile", "%"+mobile+"%"));
			}
			if(null != isMortgage){
				ca.add(Restrictions.eq("isMortgage", isMortgage));
			}
			if(!StringUtil.isEmpty(mortgageType)){
				ca.add(Restrictions.like("mortgageTypes", "%|"+mortgageType+"|%"));
			}
			if(null != minAge){
				ca.add(Restrictions.ge("age", minAge));
			}
			if(null != maxAge){
				ca.add(Restrictions.le("age", maxAge));
			}
			if(null != wage){
				ca.add(Restrictions.ge("wage", wage));
			}
			if(!StringUtil.isEmpty(createDateStart)){
				ca.add(Restrictions.ge("createDate",createDateStart));
			}
			if(!StringUtil.isEmpty(createDateEnd)){
				ca.add(Restrictions.le("createDate",createDateEnd));
			}
			if(null != pageList){
				retList	= PaginationQuery.queryCriteria(ca, pageList);
			}else{
				retList	= ca.list();
			}
		}catch(Exception e){
			throw new DaoException(className+" listOrder",e);
		}finally{
			return retList;
		}
	}
}
